<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({name: 'menu-create'})">添加菜单</el-button>
      </div>
         <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            label="编号"
            type="index"
          ></el-table-column>
          <el-table-column
            label="菜单名称"
            prop="name"
          ></el-table-column>
          <el-table-column
            label="菜单级数"
            prop="level"
          ></el-table-column>
          <el-table-column
            label="前端图标"
            prop="icon"
          ></el-table-column>
          <el-table-column
            label="排序"
            prop="orderNum"
          ></el-table-column>
          <el-table-column
            label="操作"
          >
            <template v-slot:default="scope">
              <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
  </el-card>
  </div>
</template>

<script>
import { getAllMenus, deleteMenu } from '@/services/menu'
export default {
  data () {
    return {
      tableData: []
    }
  },
  created () {
    this.loadAllMenus()
  },
  methods: {
    async loadAllMenus () {
      const { data } = await getAllMenus()
      if (data.code === '000000') {
        this.tableData = data.data
      }
    },
    handleEdit (row) {
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: row.id
        }
      })
    },
    handleDelete (row) {
      // 确认提示
      this.$confirm('确认删除吗？', '删除提示')
        .then(async () => {
          const { data } = await deleteMenu(row.id)
          if (data.code === '000000') {
            this.$message.success('删除成功')
            this.loadAllMenus()
          }
        }).catch(() => {
          this.$message.info('已取消删除')
        })
    }
  }
}
</script>

<style>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 100%;
  }
</style>
